package com.myzl.coal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.myzl.coal.domain.AgreementContrast;
import com.myzl.coal.domain.AlarmEvent;
import com.myzl.coal.domain.Device;
import com.myzl.coal.domain.ThresholdData;
import com.myzl.coal.mapper.AgreementContrastMapper;
import com.myzl.coal.mapper.ThresholdDataMapper;
import com.myzl.coal.service.IThresholdDataService;
import com.myzl.common.constant.ThresholdDescriptionConstants;
import com.myzl.common.core.domain.AjaxResult;
import com.myzl.common.enums.AlarmEventType;
import com.myzl.common.enums.CompareType;
import com.myzl.common.enums.SieveRadarCommandEnum;
import com.myzl.common.exception.CustomException;
import com.myzl.common.utils.DictUtils;
import com.myzl.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 阈值Service业务层处理
 *
 * @author myzl
 * @date 2022-11-28
 */
@Service
public class ThresholdDataServiceImpl implements IThresholdDataService
{
    @Autowired
    private ThresholdDataMapper thresholdDataMapper;

    @Autowired
    private AgreementContrastMapper agreementContrastMapper;

    /**
     * 查询阈值
     *
     * @param id 阈值主键
     * @return 阈值
     */
    @Override
    public ThresholdData selectThresholdDataById(Long id)
    {
        return thresholdDataMapper.selectThresholdDataById(id);
    }

    /**
     * 查询阈值列表
     *
     * @param thresholdData 阈值
     * @return 阈值
     */
    @Override
    public List<ThresholdData> selectThresholdDataList(ThresholdData thresholdData)
    {
        return thresholdDataMapper.selectThresholdDataList(thresholdData);
    }

    /**
     * 新增阈值
     *
     * @param thresholdData 阈值
     * @return 结果
     */
    @Override
    public int insertThresholdData(ThresholdData thresholdData)
    {
        int count = thresholdDataMapper.selectCount(new LambdaQueryWrapper<ThresholdData>().
                eq(ThresholdData::getDictValue,thresholdData.getDictValue()).
                eq(ThresholdData::getThresholdValue,thresholdData.getCompareType()));
        if (count>0){
            throw new CustomException("该阈值类型比较关系重复录入");
        }
        return thresholdDataMapper.insertThresholdData(thresholdData);
    }

    /**
     * 修改阈值
     *
     * @param thresholdData 阈值
     * @return 结果
     */
    @Override
    public int updateThresholdData(ThresholdData thresholdData)
    {
        int count = thresholdDataMapper.selectCount(new LambdaQueryWrapper<ThresholdData>().
                eq(ThresholdData::getDictValue,thresholdData.getDictValue()).
                eq(ThresholdData::getThresholdValue,thresholdData.getCompareType()).
                ne(ThresholdData::getId,thresholdData.getId()));
        if (count>0){
            throw new CustomException("该阈值类型比较关系重复录入");
        }
        return thresholdDataMapper.updateThresholdData(thresholdData);
    }

    /**
     * 批量删除阈值
     *
     * @param ids 需要删除的阈值主键
     * @return 结果
     */
    @Override
    public int deleteThresholdDataByIds(Long[] ids)
    {
        return thresholdDataMapper.deleteThresholdDataByIds(ids);
    }

    /**
     * 删除阈值信息
     *
     * @param id 阈值主键
     * @return 结果
     */
    @Override
    public int deleteThresholdDataById(Long id)
    {
        return thresholdDataMapper.deleteThresholdDataById(id);
    }

    @Override
    public AlarmEvent compareTo(Integer deviceId, BigDecimal data, String title, String content, boolean contentFlag, ThresholdData rule) {
        boolean result = false;
        BigDecimal standard = rule.getThresholdValue();
        CompareType compareType = CompareType.getByCode(rule.getCompareType());
        AlarmEvent alarmEvent = new AlarmEvent();
        alarmEvent.setDictValue(rule.getDictValue());
        alarmEvent.setThresholdDataId(rule.getId());
        alarmEvent.setDeviceId(deviceId);
        alarmEvent.setTitle(title);
        alarmEvent.setEventType(AlarmEventType.SIEVE.getCode());
        if (!contentFlag){
            alarmEvent.setContent(content);
        }
        switch (compareType) {
            case EQ:
                if (data.compareTo(standard) == 0){
                    if (contentFlag){
                        alarmEvent.setContent(content+ ThresholdDescriptionConstants.EQ + standard);
                    }
                    result = true;
                }
                break;
            case GT:
                if (data.compareTo(standard) > 0){
                    if (contentFlag){
                        alarmEvent.setContent(content+ ThresholdDescriptionConstants.GT+ standard);
                    }
                    result = true;
                }
                break;
            case GE:
                if (data.compareTo(standard) >= 0){
                    if (contentFlag){
                        alarmEvent.setContent(content+ ThresholdDescriptionConstants.GE + standard);
                    }
                    result = true;
                }
                break;
            case LT:
                if (data.compareTo(standard) < 0){
                    if (contentFlag){
                        alarmEvent.setContent(content+ ThresholdDescriptionConstants.LT + standard);
                    }
                    result = true;
                }
                break;
            case LE:
                if (data.compareTo(standard) <= 0){
                    if (contentFlag){
                        alarmEvent.setContent(content+ ThresholdDescriptionConstants.LE + standard);
                    }
                    result = true;
                }
                break;
            default:
        }
        if (result){
            return alarmEvent;
        }
        return null;
    }

    @Override
    public AjaxResult selectDeviceAgreement(List<String> deviceTypes) {
        List<AgreementContrast> agreementContrasts = agreementContrastMapper.selectList(new LambdaQueryWrapper<AgreementContrast>()
                .in(AgreementContrast::getDeviceType, deviceTypes));
        return AjaxResult.success(agreementContrasts);
    }

    @Override
    public List<ThresholdData> getThresholdByDevice(Device device) {
        List<ThresholdData> thresholdData = thresholdDataMapper.selectList(new LambdaQueryWrapper<ThresholdData>()
                .eq(ThresholdData::getDeviceId, device.getId()));
        if (thresholdData.isEmpty()){
            thresholdData = thresholdDataMapper.selectList(new LambdaQueryWrapper<ThresholdData>()
                    .eq(ThresholdData::getDeviceType, device.getDeviceType()));
        }
        return thresholdData;
    }

    @Override
    public Map<String, String> getAlarmText(ThresholdData rule) {
        String dictLabel = DictUtils.getDictLabel("binocular_radar_function", String.valueOf(rule.getDictValue()));
        dictLabel = StringUtils.isEmpty(dictLabel) ? DictUtils.getDictLabel("sieve_radar_function", String.valueOf(rule.getDictValue())) : dictLabel;
        dictLabel = "["+ dictLabel +"]:";
        Map<String,String> result = new HashMap<>(16);
        if (Objects.equals(rule.getDictValue(),SieveRadarCommandEnum.COAL_BIG_ALARM.getCode()) || Objects.equals(rule.getDictValue(),SieveRadarCommandEnum.COAL_SMALL_ALARM.getCode())){
            CompareType compareType = CompareType.getByCode(rule.getCompareType());
            switch (compareType) {
                case EQ:
                case GT:
                case GE:
                    result.put("title",dictLabel + ThresholdDescriptionConstants.THICKNESS_BIG_TITLE);
                    result.put("content",ThresholdDescriptionConstants.THICKNESS_BIG_CONTENT);
                    break;
                case LT:
                case LE:
                    result.put("title",dictLabel + ThresholdDescriptionConstants.THICKNESS_SMALL_TITLE);
                    result.put("content",ThresholdDescriptionConstants.THICKNESS_SMALL_CONTENT);
                    break;
                default:
            }

        } else if (Integer.valueOf(SieveRadarCommandEnum.UNBALANCE_ALARM.getCode()).equals(rule.getDictValue())){
            result.put("title",dictLabel + ThresholdDescriptionConstants.THICKNESS_UNBALANCE_TITLE);
            result.put("content",ThresholdDescriptionConstants.THICKNESS_UNBALANCE_CONTENT);
        } else {
            result.put("title",dictLabel);
            result.put("content","");
        }
        return result;
    }
}
